home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / windows / boyer04.zip / DEMO.C < prev    next >
C/C++ Source or Header  |  1996-01-15  |  8KB  |  228 lines

  1. /*-----------------------------------------------------------------------------
  2.     file:   demo.c
  3.     desc:   Boyer-Moore text search algorithm demo (Windows version)
  4.     by:     Patrick Ko
  5.     date:   6 Mar 91 - born
  6.     revi:   4 Apr 94 - port Windows 3.1
  7.     note:   use huge pointers to cater for big contiguous memory
  8. -----------------------------------------------------------------------------*/
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #include <memory.h>
  12.  
  13. #include "boyer.h"
  14. #include "demo.h"
  15.  
  16. long FAR PASCAL _export WndProc (HWND, UINT, UINT, LONG) ;
  17.  
  18. static char szAppName [] = "Demo" ;
  19. static char temp[128];
  20.  
  21. static HPSTR ss = NULL;
  22. static HPSTR sf = NULL;
  23. static HPSTR sb = NULL;
  24. static HPSTR sfic = NULL;
  25. static HPSTR sbic = NULL;
  26. static int msg = 0;
  27. static HFIND hfind;
  28.  
  29. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  30.                     LPSTR lpszCmdLine, int nCmdShow)
  31. {
  32.     HWND hwnd;
  33.     MSG msg;
  34.     WNDCLASS wndclass;
  35.  
  36.     if (!hPrevInstance)
  37.     {
  38.         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  39.         wndclass.lpfnWndProc   = WndProc ;
  40.         wndclass.cbClsExtra    = 0 ;
  41.         wndclass.cbWndExtra    = 0 ;
  42.         wndclass.hInstance     = hInstance ;
  43.         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  44.         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  45.         wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  46.         wndclass.lpszMenuName  = szAppName ;
  47.         wndclass.lpszClassName = szAppName ;
  48.  
  49.         RegisterClass (&wndclass) ;
  50.     }
  51.  
  52.     hwnd = CreateWindow (szAppName, "Boyer-Moore Algorithm Demonstration",
  53.            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  54.            CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ;
  55.  
  56.     ShowWindow (hwnd, nCmdShow) ;
  57.     UpdateWindow (hwnd) ;
  58.  
  59.     while (GetMessage (&msg, NULL, 0, 0))
  60.     {
  61.         TranslateMessage (&msg) ;
  62.         DispatchMessage (&msg) ;
  63.     }
  64.     return msg.wParam ;
  65. }
  66.  
  67. long FAR PASCAL _export WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam)
  68. {
  69.     HMENU hMenu;
  70.     static short cxChar, cxCaps, cyChar;
  71.     HDC hdc;
  72.     PAINTSTRUCT ps;
  73.     TEXTMETRIC tm;
  74.     long i;
  75.  
  76.     switch (message)
  77.     {
  78.         case WM_CREATE:
  79.             hdc = GetDC(hwnd);
  80.             GetTextMetrics(hdc, &tm);
  81.             cxChar = tm.tmAveCharWidth;
  82.             cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
  83.             cyChar = tm.tmHeight + tm.tmExternalLeading;
  84.             ReleaseDC(hwnd, hdc);
  85.             return 0;
  86.  
  87.         case WM_COMMAND:
  88.             hMenu = GetMenu (hwnd) ;
  89.             switch (wParam)
  90.             {
  91.                 case IDM_GO:
  92.  
  93.                     msg = 1;
  94.                     InvalidateRect(hwnd, NULL, FALSE);
  95.                     SendMessage(hwnd, WM_PAINT, 0, 0L);
  96.  
  97.                     /* allocate around 1M space */
  98.                     ss = (char huge *)GlobalAllocPtr(GMEM_MOVEABLE, SPACE_1M);
  99.  
  100.                     /* initialize the space with texts */
  101.                     for (i=0; i<SPACE_1M; i+=4)
  102.                     {
  103.                         /*
  104.                          * only apply in large+ model where memcpy is
  105.                          * large by default
  106.                         */
  107.                         memcpy( (HPSTR)ss + i, "wild", 4 );
  108.                     }
  109.  
  110.                     /* put our targets "tiger" and "TIGER" */
  111.  
  112.                     memcpy( (HPSTR)ss + tiger_POSITION, "tiger", 5 );
  113.                     memcpy( (HPSTR)ss + TIGER_POSITION, "TIGER", 5 );
  114.  
  115.                     msg++;
  116.                     InvalidateRect(hwnd, NULL, FALSE);
  117.                     SendMessage(hwnd, WM_PAINT, 0, 0L);
  118.  
  119.                     /* setup what you want to search */
  120.                     hfind = SetFindPattern( "tiger" );
  121.  
  122.                     /* search string in forward manner */
  123.                     sf = Find(hfind, (HPSTR)ss, SPACE_1M);
  124.  
  125.                     /* search string in backward manner */
  126.                     /* be careful not to specify a "huge pointer" outside */
  127.                     /* what you have allocated in Windows, since Windows */
  128.                     /* huge library does some work to translate your ptr */
  129.                     /* into the GlobalAlloc() space */
  130.                     sb = FindBackward(hfind, (HPSTR)ss + SPACE_1M - 1, SPACE_1M);
  131.  
  132.                     /* search string in case insensitive forward manner */
  133.                     sfic = FindIC(hfind, (HPSTR)ss, SPACE_1M);
  134.  
  135.                     /* search string in backward manner */
  136.                     /* be careful not to specify a "huge pointer" outside */
  137.                     /* what you have allocated in Windows, since Windows */
  138.                     /* huge library does some work to translate your ptr */
  139.                     /* into the GlobalAlloc() space */
  140.                     sbic = FindBackwardIC(hfind, (HPSTR)ss + SPACE_1M - 1, SPACE_1M);
  141.  
  142.                     GlobalFreePtr(ss);
  143.                     FreeFindPattern(hfind);
  144.  
  145.                     msg++;
  146.                     InvalidateRect(hwnd, NULL, FALSE);
  147.                     SendMessage(hwnd, WM_PAINT, 0, 0L);
  148.  
  149.                     return 0 ;
  150.  
  151.                 case IDM_EXIT:
  152.                     SendMessage (hwnd, WM_CLOSE, 0, 0L) ;
  153.                     return 0 ;
  154.  
  155.                 case IDM_ABOUT:
  156.                     MessageBox (hwnd, "Boyer-Moore Algorithm Demo",
  157.                              szAppName, MB_ICONINFORMATION | MB_OK) ;
  158.                     return 0 ;
  159.             }
  160.             break ;
  161.  
  162.         case WM_PAINT:
  163.             switch (msg)
  164.             {
  165.                 case 1:
  166.                     hdc = BeginPaint(hwnd, &ps);
  167.                     wsprintf(temp, "initializing search space with \"wild tiger and TIGER\"");
  168.                     TextOut(hdc, cxChar, cyChar * 1, temp, lstrlen(temp));
  169.                     EndPaint(hwnd, &ps);
  170.                     break;
  171.  
  172.                 case 2:
  173.                     hdc = BeginPaint(hwnd, &ps);
  174.                     wsprintf(temp, "begin search:");
  175.                     TextOut(hdc, cxChar, cyChar * 2, temp, lstrlen(temp));
  176.                     wsprintf(temp, "searching for word \"tiger\" in 1M space:");
  177.                     TextOut(hdc, cxChar, cyChar * 3, temp, lstrlen(temp));
  178.                     EndPaint(hwnd, &ps);
  179.                     break;
  180.  
  181.  
  182.                 case 3:
  183.                     if (ss == NULL) break;
  184.  
  185.                     hdc = BeginPaint(hwnd, &ps);
  186.  
  187.                     if (sf == NULL)
  188.                         wsprintf(temp, "Find() pattern not found");
  189.                     else
  190.                         wsprintf(temp, "Find() pattern found at offset = %lu",
  191.                             (DWORD)((HPSTR)sf - (HPSTR)ss));
  192.                     TextOut(hdc, cxChar, cyChar * 4, temp, lstrlen(temp));
  193.  
  194.                     if (sb == NULL)
  195.                         wsprintf(temp, "FindBackward() pattern not found");
  196.                     else
  197.                         wsprintf(temp, "FindBackward() pattern found at offset = %lu",
  198.                             (DWORD)((HPSTR)((HPSTR)ss + SPACE_1M - 1) - (HPSTR)sb));
  199.                     TextOut(hdc, cxChar, cyChar * 5, temp, lstrlen(temp));
  200.  
  201.                     if (sfic == NULL)
  202.                         wsprintf(temp, "FindIC() pattern not found");
  203.                     else
  204.                         wsprintf(temp, "FindIC() pattern found at offset = %lu",
  205.                             (DWORD)((HPSTR)sfic - (HPSTR)ss));
  206.                     TextOut(hdc, cxChar, cyChar * 6, temp, lstrlen(temp));
  207.  
  208.                     if (sbic == NULL)
  209.                         wsprintf(temp, "FindBackwardIC() pattern not found");
  210.                     else
  211.                         wsprintf(temp, "FindBackwardIC() pattern found at offset = %lu",
  212.                             (DWORD)((HPSTR)((HPSTR)ss + SPACE_1M - 1) - (HPSTR)sbic));
  213.                     TextOut(hdc, cxChar, cyChar * 7, temp, lstrlen(temp));
  214.                     EndPaint(hwnd, &ps);
  215.                     break;
  216.  
  217.                 default:
  218.                     break;
  219.             }
  220.             break;
  221.  
  222.         case WM_DESTROY :
  223.             PostQuitMessage (0) ;
  224.             return 0 ;
  225.     }
  226.     return DefWindowProc (hwnd, message, wParam, lParam) ;
  227. }
  228.